import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '后台首页', icon: 'dashboard' }
    }]
  },

  // 系统管理
  {
    path: '/system',
    component: Layout,
    redirect: '/system/users',
    name: 'System',
    meta: { title: '系统管理', icon: 'el-icon-s-help' },
    children: [
      // 用户管理
      {
        path: 'users',
        name: 'Users',
        component: () => import('@/views/Users/index'),
        meta: { title: '用户管理', icon: 'table' }
      },
      // 权限管理
      {
        path: 'powers',
        name: 'Powers',
        component: () => import('@/views/Powers/index'),
        meta: { title: '权限管理', icon: 'tree' }
      },
      // 角色管理
      {
        path: 'department',
        name: 'Department',
        component: () => import('@/views/Department/index'),
        meta: { title: '部门管理', icon: 'tree' }
      },
      // 日志管理
      {
        path: 'logs',
        name: 'Logs',
        component: () => import('@/views/Logs/index'),
        meta: { title: '日志管理', icon: 'tree' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  //  重置路由
  router.matcher = newRouter.matcher
}

export default router
